package org.taishan.electronshop.dao;

import org.taishan.electronshop.domain.Customer;
import org.taishan.electronshop.domain.Goods;
import org.taishan.electronshop.domain.TrolleyItem;
import org.taishan.electronshop.util.JdbcHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TrolleyDao implements Dao<TrolleyItem,Integer> {
    private TrolleyDao td;
    private CustomerDao cd = new CustomerDao();
    private GoodsDao gd = new GoodsDao();
    private static final String table = " t_trolleyitems ";

    /**
     * 根据购物车项的id，进行查询,查询结果放入一个对象中
     * @param id 数据库中的主键
     * @return
     */
    @Override
    public TrolleyItem load(Integer id) {
        String SQL = "SELECT   id,goods_id,goods_name,goods_image,goods_price,customer_id, goods_number FROM" + table + "WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL, id);
        helper.destory();
        if (list != null && list.size() == 1) {
            Map<String, Object> map = list.get(0);
            TrolleyItem t = this.cast(map);
            return t;
        }

        return null;
    }
    /**
     * 根据购物车项的中的商品名字和顾客id，进行查询,查询结果放入一个对象中
     * @param name 数据库中的主键
     * @return
     */
    public TrolleyItem load(String name,Integer cId) {
        String SQL = "SELECT   id,goods_id,goods_name,goods_image,goods_price,customer_id, goods_number FROM" + table +"WHERE goods_name = ? and customer_id = ? ";
        System.out.println(name);
        System.out.println(cId);
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL, name,cId);
        helper.destory();
        if (list != null && list.size() == 1) {
            Map<String, Object> map = list.get(0);
            TrolleyItem t = this.cast(map);
            return t;
        }
        return null;
    }

    /**
     * 查询数据库中所有购物车项
     * @return
     */
    @Override
    public List<TrolleyItem> list() {
        String SQL = "SELECT   goods_id,goods_name,goods_price,customers_id, goods_number FROM" + table;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL);
        helper.destory();
        if (list != null && list.size() > 0) {
            List<TrolleyItem> trolleyItems = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                TrolleyItem t = this.cast(map);
                trolleyItems.add(t);
            }
            return trolleyItems;
        }
        return null;
    }

    /**
     * 查询数据库中属于顾客id等于userid的所有购物车项
     * @return
     */

    public List<TrolleyItem> list(Integer userid) {
        String SQL = "SELECT   id,goods_id,goods_name,goods_image,goods_price,customer_id, goods_number FROM" + table + " WHERE customer_id = ? ";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String, Object>> list = helper.query(SQL,userid);
        helper.destory();
        if (list != null && list.size() > 0) {
            List<TrolleyItem> trolleyItems = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = list.get(i);
                TrolleyItem t = this.cast(map);
                trolleyItems.add(t);
            }
            return trolleyItems;
        }
        return null;
    }


    /**
     * 将数据存入数据库，即将商品放入购物车中
     * @param t
     * @return
     */
    @Override
    public boolean save(TrolleyItem t) {
        String SQL = "INSERT INTO " + table +
                " ( goods_id , goods_name ,goods_image , goods_price,goods_number , customer_id  ) " +
                " VALUES (  ? , ? , ? , ? ,? ,? )";
        Goods g = t.getGoods();
        Integer goodsId = g.getId();
        String goodsName = t.getName();
        String goodsImage = t.getImage();
        double goodsPrice = t.getPrice();
        int goodsNumber = t.getNumber();
        Customer c = t.getCustomer();
        int cId =c.getId();
        /*
        判断数据库中这个顾客是否有这个商品,没有则新增一条数据，有则修改那天数据令其number加1
         */
        // td.load(goodsName)
        td = new TrolleyDao();
//        if (td.load(goodsName,cId) == null ) {
//            goodsNumber = 1;
//        } else if(td.load(goodsName,cId).getNumber() >=1){
//            goodsNumber = td.load(goodsName,cId).getNumber() + 1;
//            t = td.load(goodsName,cId);
//            return td.update(t);
//        }else if(td.load(goodsName,cId).getNumber() <1){
//            td.delete(td.load(goodsName,cId).getId());
//        }
        Object[] array = {goodsId, goodsName,goodsImage, goodsPrice,goodsNumber, cId};
        JdbcHelper helper = new JdbcHelper();
        // 执行插入语句，并获得由数据库产生的主键值
        int id = helper.insert(SQL, array);
        t.setId(id); // 将由数据库产生的主键值设置到 Customer 对象的 id 字段上
        helper.destory();
        // 如果 id 不等于 -1  则视为保存成功( 在 JdbcHelper 的 insert 方法中有约定 )
        return id != -1;

    }


    /**
     * 传入一个对象t，根据这个对象的数据，对数据库里对应的数据进行更改
     * @param t
     * @return
     */
    @Override
    public boolean update(TrolleyItem t) {
        String SQL = "UPDATE " + table +
                " SET goods_number = ? " +
                " WHERE id = ? ";
        int number = t.getNumber();
        int id = t.getId();
//        if (number <= 0) {
//            td.delete(id);
//        }
        Object[] params = {number, id};

        JdbcHelper helper = new JdbcHelper();
        int count = helper.update(SQL, params);
        helper.destory();
        return count == 1;
    }

//    public boolean update(TrolleyItem t) {
//        String SQL = "UPDATE " + table +
//                " SET goods_number = ? " +
//                " WHERE id = ? ";
//        int number = t.getNumber();
//        int id = t.getId();
//        if (number <= 0) {
//            td.delete(id);
//        }
//        Object[] params = {number, id};
//
//        JdbcHelper helper = new JdbcHelper();
//        int count = helper.update(SQL, params);
//        helper.destory();
//        return count == 1;
//    }

    /**
     *根据购物车项的id执行删除操作
     * @param id 被删除记录的主键值
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        String SQL = "DELETE FROM " + table + " WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update(SQL, id);
        helper.destory();
        return count == 1;
    }


    /**
     * 将从数据库中获取的数据存入到TrolleyItem对象中
     * @param map 需要处理的 map 集合
     * @return
     */
    @Override
    public TrolleyItem cast(Map map) {
        TrolleyItem t = new TrolleyItem();

        Customer c = new Customer();
        t.setId((Integer) map.get("id"));
        t.setName((String) map.get("goods_name"));
        t.setPrice((Double) map.get("goods_price"));
        t.setImage((String)map.get("goods_image"));
        String gName = t.getName();
        Goods g=gd.load(gName);
        t.setGoods(g);
        c.setId((Integer) map.get("customer_id"));
        int cId= c.getId();
        c= cd.load(cId);
        t.setCustomer(c);
        t.setnumber((Integer) map.get("goods_number"));
        return t;
    }



}
